﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Company;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Log;


namespace Jzy.Web
{
    public partial class CompanyLogin : System.Web.UI.Page
    {
        private UserBO Users
        {
            get
            {
                return UserBO.Instance<UserBO>();
            }
        }


       
        private string strFirstModuleCode;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!IsPostBack)
                {
                    if (AppSetting.MultiCompany == false)
                    {
                        Response.Write("配置文件不支持集团版，请检查");
                        Response.End();
                    }
                   
                    string strLoginName = Request.QueryString["LoginName"];
                    strFirstModuleCode = Request.QueryString["ModuleCode"];
                    if (strLoginName != null)
                    {
                        string strPassword = Request["Password"];
                        strPassword = StrUtil.DecryptDES(strPassword);
                        CheckLogin(strLoginName, strPassword);
                    }

                    txtLoginName.Text = AppCookie.LoginName;
                    txtCompanyLoginName.Text = AppCookie.CompanyLoginName;
                }
            }
            catch (Exception ex)
            {
                JSUtil.Alert(ex.Message);
            }
        }
        
        private void CheckLogin(string strLoginName, string strPassword)
        {

            CompanyBO company = new CompanyBO();
            company.dbOper.ConnectionString = AppSetting.ConnectionString;
       
            string companyLoginName = txtCompanyLoginName.Text.Trim();

            CompanyInfo comInfo = company.GetInfoByLoginName(companyLoginName);

            if (comInfo == null)
            {
                JSUtil.Alert("公司名不存在");
                return;
            }
            else
            {
                PageBase.SetCompanySession(comInfo);
            }

            LoginResult result = Users.CheckLogin(strLoginName, strPassword);

            bool blnFlag = true;
            switch (result)
            {
                case LoginResult.Forbidden:
                case LoginResult.PwdErr:
                case LoginResult.UserErr:
                    blnFlag = false;
                    break;
            }

            if (blnFlag)
            {
                PageBase.SetSession(strLoginName);
            }
            else
            {
                AppSession.LoginName = strLoginName;//为写日志用的
                JSUtil.Alert("用户名或密码错！");
            }

            int userID = 0;
            if (!string.IsNullOrEmpty(AppSession.LoginName))
            {
                userID = AppSession.UserID;
            }

            Logger.Login(userID, AppSession.LoginName, AppSession.UserName, Request.UserHostAddress, (int)result);

            //blnFlag = true;
            if (blnFlag)
            {
                // LogMessageBO.Write("login", "test");
                string strUrl = "common/frame/default.aspx";
                if (strFirstModuleCode != null)
                {
                    strUrl += "?ModuleCode=" + strFirstModuleCode;
                }
                Response.Redirect(strUrl);
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string strLoginName = txtLoginName.Text.Trim();
            string strPassword = txtPassword.Text.Trim();


            CheckLogin(strLoginName, strPassword);
        }

       
    }
}